//===========================================================================
// if player (AIMP or WinAmp) is not running, it will be launched and playback started;
// if player is already running, this app acts as play/pause toggle
//===========================================================================


#include <windows.h>
#include "..\helpers.h"
#include "..\FRONTEND.H"


#define WINAMP_IS_STOPPED	0
#define WINAMP_IS_PLAYING	1
#define WINAMP_IS_PAUSED	3


//===========================================================

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	// search for Winamp main window
	HWND hwndWinAmp=FindWindow("Winamp v1.x", NULL);
	if(!IsWindow(hwndWinAmp)){
		// launch player
		hwndWinAmp=StartPlayer();
		if(hwndWinAmp==NULL)
		{
			MessageBeep(MB_ICONERROR);
			return 1;	// can't start player
		}

		// play!
		PostMessage(hwndWinAmp, WM_COMMAND, WINAMP_BUTTON2, 0);
	}else{
		// winamp is already runs
		// check status
		switch(SendMessage(hwndWinAmp,WM_WA_IPC,0,IPC_ISPLAYING)){
			case WINAMP_IS_STOPPED:
				// play!
				PostMessage(hwndWinAmp, WM_COMMAND, WINAMP_BUTTON2, 0);
				break;
			case WINAMP_IS_PLAYING:
				// pause!
				PostMessage(hwndWinAmp, WM_COMMAND, WINAMP_BUTTON3, 0);
				break;
			case WINAMP_IS_PAUSED:
				// pause! (resume playback)
				PostMessage(hwndWinAmp, WM_COMMAND, WINAMP_BUTTON3, 0);
				break;
		}
	}

	return 0;	// ok
}



